#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>

using namespace std;

class Computer{
public:
    //无参
    Computer():_name(nullptr),_price(0){}
    //有参
    Computer(const char*name, double price):_name(new char[strlen(name) + 1]),_price(price){
        strcpy(_name,name);
        total_price += price;
    }
    //拷贝
    Computer(const Computer &rsh){
        _price = rsh._price;
        _name = new char[strlen(rsh._name)+1];
        strcpy(_name,rsh._name);
        total_price += rsh._price;

    }
    //赋值
    Computer &operator=(const Computer &rsh){
        if(this != &rsh){
            if(_name){
                delete [] _name;
            }
            _name = new char[strlen(rsh._name)+1];
            strcpy(_name,rsh._name);
            total_price += rsh._price - _price;
            _price = rsh._price;
            cout<<"update"<<endl;

        }
        return *this;
    }

    ~Computer(){
        if(_name){
            delete [] _name;
            _name =nullptr;
            total_price -= _price;
        }
    }
    
    static double getTotalPrice(void){
        return total_price;
    }

    void setPrice(double newPrice){
        total_price += newPrice - _price;
        _price = newPrice ;

    }

    void Print() const{
        cout<<_name<<" : "<<_price<<endl;
    }
private:
    char *_name;
    double _price;
    static double total_price;


};
double Computer::total_price = 0;

int main()
{
    cout<<Computer::getTotalPrice()<<endl;   
    Computer c1("nihao",3000);
    c1.Print();
    cout<<Computer::getTotalPrice()<<endl;   
    Computer c2(c1);
    c2.Print();
    cout<<Computer::getTotalPrice()<<endl;   
    Computer c3("chuizi",800);
    c3.Print();
    cout<<Computer::getTotalPrice()<<endl;   
    c3=Computer("8848",8848);
    c3.Print();
    cout<<Computer::getTotalPrice()<<endl;   
    
    return 0;
}

